<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="600px" :title="`${drawerProps.title}`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <!-- <el-form-item label="用户头像" prop="avatarUrl">
        <UploadImg v-model:image-url="drawerProps.row!.avatarUrl" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item> -->
      <!-- <el-form-item label="用户照片" prop="photo">
        <UploadImgs v-model:file-list="drawerProps.row!.photo" height="140px" width="140px" border-radius="50%">
          <template #empty>
            <el-icon><Picture /></el-icon>
            <span>请上传照片</span>
          </template>
          <template #tip> 照片大小不能超过 5M </template>
        </UploadImgs>
      </el-form-item> -->
      <!-- <el-form-item label="手机号" prop="mobile">
        <el-input v-model="drawerProps.row!.mobile" placeholder="请填写手机号" clearable></el-input>
      </el-form-item> -->
      <el-form-item label="变更理由" prop="typeId">
        <el-select v-model="drawerProps.row!.typeId" placeholder="请选择理由" clearable>
          <el-option v-for="item in drawerProps.integralDetail" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="角色" prop="roleId">
        <el-select v-model="drawerProps.row!.roleId" :multiple="true" placeholder="请选择角色" clearable>
          <el-option v-for="item in drawerProps.userRoleList" :key="item.name" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写用户姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-show="drawerProps.title == '新增'">
        <el-input type="password" v-model="drawerProps.row!.password" placeholder="请填写用户密码" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="drawerProps.row!.sex" placeholder="请选择性别" clearable>
          <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="drawerProps.row!.email" placeholder="请填写邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item label="省市区" prop="address">
        <el-cascader v-model="drawerProps.row!.address" :options="areaList" clearable :props="defaultOptions" ref="cascaderRef" />
      </el-form-item> -->
      <el-form-item label="积分" prop="integralNum">
        <el-input v-model="drawerProps.row!.integralNum" placeholder="请填写积分" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item label="冻结积分" prop="iceIntegral">
        <el-input v-model="drawerProps.row!.iceIntegral" placeholder="请填写冻结积分" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item> -->
      <!-- <el-form-item v-show="drawerProps.isView" label="修改人" prop="modifiedBy">
        <el-input v-model="drawerProps.row!.modifiedBy" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="修改时间" prop="gmtModified">
        <el-input v-model="drawerProps.row!.gmtModified" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建人" prop="createBy">
        <el-input v-model="drawerProps.row!.createBy" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建时间" prop="gmtCreate">
        <el-input v-model="drawerProps.row!.gmtCreate" placeholder="" clearable></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="详细地址" prop="address">
        <el-input v-model="drawerProps.row!.address" placeholder="请填写居住地址" clearable></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive, onMounted } from "vue"; // getCurrentInstance
// import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User, Dict } from "@/api/interface";
// import UploadImg from "@/components/Upload/Img.vue";
// import { getDistrict } from "@/api/modules/sys"; //getDict
// const cxt = getCurrentInstance();
// const dict = cxt?.appContext.config.globalProperties.$dict;
// import { get } from "@/api/modules/sys"; // exportUserInfo,BatchAddUser

const rules = reactive({
  typeId: [{ required: true, message: "请选择变更理由" }],
  integralNum: [{ required: true, pattern: /^[0-9]+$/, message: "只能为数字" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  integralDetail: Dict.ResDictList[];
  isAdd: boolean;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {
    roleId: []
  },
  integralDetail: [],
  isAdd: false
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  params.row.roleId = params.row.roleId || [];
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!({
        typeId: drawerProps.value.row!.typeId,
        isAdd: drawerProps.value.isAdd,
        integralNum: Number(drawerProps.value.row!.integralNum),
        userId: drawerProps.value.row.id
      });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
// const getDistricts = async (params: Area) => {
//   const { data } = await getDistrict(params);
//   return data || [];
// };
onMounted(async () => {
  // console.log(getDistricts);
});
</script>
